package com.sendi.annotation.tool;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.sendi.annotation.report.EnumCovert;
import com.sendi.annotation.report.ReportColunm;
import com.sendi.annotation.report.ReportFun;
import com.sendi.sql.DataSource;
import com.sendi.sql.FieldInf;
import com.sendi.sql.GroupByUtil;
public class ReportFacory {
	
			
	
		 public static ArrayList<DataSource> Report( List source)
		 {
			 
			 	if(source.size()>0)
			 	{
		 		 	LinkedHashMap<String, FieldInf> allFieldInf=new LinkedHashMap<String, FieldInf>(); //所有的数据列信息
					ArrayList<FieldInf> allFieldInfinlist=new ArrayList<FieldInf>();
					ArrayList<FieldInf> groupActionfileds=new ArrayList<FieldInf>();
					ArrayList<FieldInf> groupbyfieldss=new ArrayList<FieldInf>();
				 	Class c=source.get(0).getClass();
			 		String rankfield=null;
				 	String secondgroupy=null;
				 	String finalinvokemethod=null;
				 	/*
				 	 * 获取 ReportFun 元注释 
				 	 */
				 	ReportFun rf=(ReportFun)c.getAnnotation(ReportFun.class);
				 	if(rf!=null)
				 	{
				 		rankfield=rf.rankfield();
					 	secondgroupy=rf.secondgroupy();
					 	finalinvokemethod=rf.finalinvokemethod();
					 	
				  	}
				 	
				 	/*
				 	 * 获取ReportColunm 注释
				 	 */
				 	
				   	Field [] allFeild=c.getDeclaredFields();
				   	
				 	for(Field field:allFeild)
				 	{
				 		ReportColunm rc=field.getAnnotation(ReportColunm.class);
				 		if(rc!=null)
				 		{
				 			FieldInf f=new FieldInf();
				 			f.setFieldname(field.getName());
				 	 		f.setFieldType(EnumCovert.CoverFieldTyeToString(rc.fieldtype()));
				 			f.setGrouby(rc.isgroupby());
				 			f.setGroupByAction(GroupByUtil.JudgeGroupActionType(EnumCovert.CoverGroupAction(rc.groupbyaction())));
							f.setGroupByActionAcceptField(rc.groupbyactionacceptfield());
							f.setGroupbyType(EnumCovert.CovertGroupByType(rc.groupbytype()));
							f.setGroupByActionPointMethod(rc.assignmentmethdname());
							f.setGroupByActionPointfield(rc.groupbyactionpointfiled());
							allFieldInf.put(f.getFieldname(), f);
							allFieldInfinlist.add(f);
							if(f.getGroupByAction()!=-1)
							{
								groupActionfileds.add(f);//f.getGroupByAction()!=-1 说明是具有统计动作的列
								if(f.getGroupByAction()==GroupByUtil.CountAction)
								{
									f.setCountdetail(rc.countdetail());
								}
							}
							if(f.isGrouby()) //如果是分组的字段，则加到groupbyfieldss中
							{
						 		groupbyfieldss.add(f);
							 
							}
				 			
				 		}
				 		 
				 	}
				 	for(FieldInf fi:allFieldInfinlist)
					{
					 	if(fi.getGroupByAction()!=-1&&!fi.isGrouby())
						{
					 		 String acepfiled=fi.getGroupByActionAcceptField();
							 String pointfiles[]=fi.getGroupByActionPointfield();
							 ArrayList<FieldInf> GroupByActionPointFiledobj=new ArrayList<FieldInf>();
							 GroupByActionPointFiledobj.add(allFieldInf.get(acepfiled));
							 if(pointfiles!=null&&pointfiles.length>0)
							 {
								 for(String ii:pointfiles)
								 {
									 
								 	 GroupByActionPointFiledobj.add(allFieldInf.get(ii));
								 } 
							 }
						 	
							 fi.setGroupByActionPointFiledobj(GroupByActionPointFiledobj);
						}
						
					}
			 		GroupByUtil groupByUtil=null;
				 	groupByUtil=new GroupByUtil((ArrayList<DataSource> )source,groupActionfileds,groupbyfieldss,allFieldInf);
					groupByUtil.setRankfield(rankfield);
					groupByUtil.setSecondGroupy(secondgroupy);
					groupByUtil.setFinalinvokemethod(finalinvokemethod);
					try
					{
						groupByUtil.MakeGroupReult();
					}catch (Exception e) {
				 	}
					
					return groupByUtil.groupReult;
			 	}
			 	else
			 	{
			 		return null;
			 	}
	 	 }
	 
}
